// ToolBox.cpp : implementation file
//

#include "stdafx.h"
#include "ansieditor.h"
#include "ToolBox.h"
#include "MyMenu.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CToolBox dialog


CToolBox::CToolBox()
{
	//{{AFX_DATA_INIT(CToolBox)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	
}


void CToolBox::DoDataExchange(CDataExchange* pDX)
{
	CDialogBar::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CToolBox)
	DDX_Control(pDX, IDC_TXT_CHAR, m_txtChar);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CToolBox, CDialogBar)
	//{{AFX_MSG_MAP(CToolBox)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_SIZE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_INITDIALOG, OnInitDialog)
//	ON_MESSAGE(WM_SETMESSAGESTRING, OnSetMessageString)
//	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
//	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CToolBox message handlers

LONG CToolBox::OnInitDialog ( UINT wParam, LONG lParam)
{
	if ( !HandleInitDialog(wParam, lParam) || !UpdateData(FALSE))
	{
		return FALSE;
	}
	CString sChar = gpApp->GetProfileString("Data", "Stamp Char", gpApp->m_mui.LoadString(IDS_DEFAULT_STAMPCHAR));
	m_txtChar.SetWindowText(sChar);

	return TRUE;
}

void CToolBox::OnButton1() 
{
	GetAppContext()->m_pView->SetGridColor(RGB(128, 128, 128), RGB(0, 0, 0));
	GetAppContext()->m_pView->Invalidate();
}

int CToolBox::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialogBar::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD|WS_VISIBLE|CBRS_SIZE_FIXED|CBRS_TOOLTIPS|CBRS_FLYBY, CRect(0, 0, 0, 0), 1);
	m_wndToolBar.LoadToolBar(IDR_TOOLBOX);
	//
	CBitmap bmToolBox;
	bmToolBox.Attach(::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP_TOOLBOXHI), 
								 IMAGE_BITMAP, 0, 0, 0));
	m_ilToolBox.Create(18, 18, ILC_COLOR24|ILC_MASK, 10, 0);
	m_ilToolBox.Add(&bmToolBox, RGB(192, 192, 192));
	m_wndToolBar.GetToolBarCtrl().SetImageList(&m_ilToolBox);
	//
	

	m_wndToolBar1.CreateEx(this, TBSTYLE_FLAT, WS_CHILD|WS_VISIBLE|CBRS_SIZE_FIXED|CBRS_TOOLTIPS|CBRS_FLYBY, CRect(0, 0, 0, 0), 1);
	m_wndToolBar1.LoadToolBar(IDR_TOOLBOX1);
	//
	CBitmap bmToolBox1;
	bmToolBox1.Attach(::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP_TOOLBOXHI1), 
								 IMAGE_BITMAP, 0, 0, 0));
	m_ilToolBox1.Create(18, 18, ILC_COLOR24|ILC_MASK, 10, 0);
	m_ilToolBox1.Add(&bmToolBox1, RGB(192, 192, 192));
	m_wndToolBar1.GetToolBarCtrl().SetImageList(&m_ilToolBox1);
	//

	m_wndToolBar.SetColumns(2);

	SetWindowPos(0, 0, 0, 58, 200, SWP_NOMOVE|SWP_NOZORDER);

	return 0;
}

void CToolBox::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
//	CRect rc;
//	GetClientRect(rc);
//	dc.FillSolidRect(rc, RGB(0, 255, 0));
	// Do not call CDialogBar::OnPaint() for painting messages
}

//BOOL CToolBox::OnToolTipText(UINT n, NMHDR* pNMHDR, LRESULT* pResult)
//{
//	m_nHoverID = pNMHDR->idFrom;
////	if(m_nHoverID >= ID_TOOL_ARROW && m_nHoverID <= ID_TOOL_VERTTRANS)
////		SetStatusText(m_nHoverID);
//
//	return gpFrame->OnToolTipText(n, pNMHDR, pResult);
//	
//}
//
//int CToolBox::OnToolHitTest (CPoint point, TOOLINFO* pTI) const
//{
//	int nHit = CWnd::OnToolHitTest(point, pTI);
//	if(nHit == 1)
//	{
//		return m_nHoverID;
//	}
//	return nHit;
//}
//
//void CToolBox::SetToolBarColumns(int nColumns)
//{
//	int nCount = m_wndToolBar.GetToolBarCtrl().GetButtonCount();
//
//	if(nColumns == 0)
//	{
//		for(int i = 0; i < nCount; i++)
//		{
//			UINT nStyle = m_wndToolBar.GetButtonStyle(i);
//			nStyle &= ~TBBS_WRAPPED;
//			m_wndToolBar.SetButtonStyle(i, nStyle);
//		}
//	}
//	else
//	{
//		for(int i = 0; i < nCount; i++)
//		{
//			UINT nStyle = m_wndToolBar.GetButtonStyle(i);
//			BOOL bWrap = (((i + 1) % nColumns) == 0);
//			if (bWrap)
//				nStyle |= TBBS_WRAPPED;
//			else
//				nStyle &= ~TBBS_WRAPPED;
//
//			m_wndToolBar.SetButtonStyle(i, nStyle);
//		}
//	}
//}

CSize CToolBox::CalcDynamicLayout(int nLength, DWORD dwMode)
{
	CSize size = CDialogBar::CalcDynamicLayout(nLength, dwMode);
	if(dwMode & LM_VERTDOCK)
	{
		size.cx = 58;
		size.cy = 200;
	}
	else
	{
		size.cx = 362;
		size.cy = 32;
	}
	return size;	
}

void CToolBox::OnSize(UINT nType, int cx, int cy) 
{
	CDialogBar::OnSize(nType, cx, cy);
	
	CWnd *pWndSep1 = GetDlgItem(IDC_STATIC_SEP1);
	if(!pWndSep1->GetSafeHwnd())
	{
		return;
	}
	if(::IsWindow(m_wndToolBar.m_hWnd))
	{
		if(cx < cy)
		{
			m_wndToolBar.SetColumns(2);
			m_wndToolBar.MoveWindow(4, 4, 50, 125);
			pWndSep1->MoveWindow(4, 132, 50, 2);
			m_wndToolBar1.MoveWindow(4, 138, 50, 30);
			m_txtChar.MoveWindow(4, 167, 49, 20);
		}
		else
		{
			m_wndToolBar.SetColumns(0);
			m_wndToolBar.MoveWindow(4, 4, 250, 30);
			pWndSep1->MoveWindow(258, 3, 2, 27);
			m_wndToolBar1.MoveWindow(263, 4, 50, 30);
			m_txtChar.MoveWindow(318, 8, 38, 20);
		}
		
	}
}

CWnd* CToolBox::GetMessageBar()
{
	return AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR, TRUE);
}

//LRESULT CToolBox::OnSetMessageString(WPARAM wParam, LPARAM lParam)
//{
//	return gpFrame->SendMessage(WM_SETMESSAGESTRING, wParam);
//	CFrameWnd* pFrameWnd = (CFrameWnd*)AfxGetMainWnd();
//	UINT nIDLast = 0;//pFrameWnd->m_nIDLastMessage;
//	m_nFlags &= ~WF_NOPOPMSG;
//
//	CWnd* pMessageBar = GetMessageBar();
//	if (pMessageBar != NULL)
//	{
//		LPCTSTR lpsz = NULL;
//		CString strMessage;
//
//		// set the message bar text
//		if (lParam != 0)
//		{
//			ASSERT(wParam == 0);    // can't have both an ID and a string
//			lpsz = (LPCTSTR)lParam; // set an explicit string
//		}
//		else if (wParam != 0)
//		{
//			// map SC_CLOSE to PREVIEW_CLOSE when in print preview mode
////			if (wParam == AFX_IDS_SCCLOSE && m_lpfnCloseProc != NULL)
////				wParam = AFX_IDS_PREVIEW_CLOSE;
//
//			// get message associated with the ID indicated by wParam
//			pFrameWnd->GetMessageString(wParam, strMessage);
//			lpsz = strMessage;
//		}
//		pMessageBar->SetWindowText(lpsz);
//
//		// update owner of the bar in terms of last message selected
////		pFrameWnd->m_nIDLastMessage = 0;//(UINT)wParam;
////		pFrameWnd->m_nIDTracking = (UINT)wParam;
//	}
//
////	m_nIDLastMessage = (UINT)wParam;    // new ID (or 0)
////	m_nIDTracking = (UINT)wParam;       // so F1 on toolbar buttons work
//	return nIDLast;
//}

BOOL CToolBox::SetStatusText(int nHit)
{
	return FALSE;
}

void CToolBox::OnDestroy() 
{
	CDialogBar::OnDestroy();
	
	CString sChar;
	m_txtChar.GetWindowText(sChar);
	gpApp->WriteProfileString("Data", "Stamp Char", sChar);
}

BOOL CToolBox::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
	{
		GetAppContext()->m_pView->SetCurTool(TOOL_ARROW);
		GetAppContext()->m_pView->SetFocus();
	}
	return CDialogBar::PreTranslateMessage(pMsg);
}

void CToolBox::OnCharset() 
{
	CToolBar *ptb = &m_wndToolBar1;
	ptb->SendMessage(TB_PRESSBUTTON, ID_CHARSET, MAKELONG(1, 0));
	CMyMenu menu;
	menu.CreatePopupMenu();
	
	int nCount = GetAppContext()->m_pCharBox->m_arrCharSetName.GetSize();
	for(int n = 0; n < nCount; n++)
	{
		CString sCharSetName = GetAppContext()->m_pCharBox->m_arrCharSetName.GetAt(n);
		menu.AppendMenu(IDM_CHARSET_BASE + n, sCharSetName);
	}
	menu.AppendSeparator();
	menu.AppendMenu(IDM_CHARSET_CONFIG);
	
	CRect rc;
	ptb->SendMessage(TB_GETRECT, ID_CHARSET, (LPARAM)&rc);
	ptb->ClientToScreen(&rc);

	menu.SetDefaultItem(GetAppContext()->m_pCharBox->m_nCurCharSet + IDM_CHARSET_BASE);
	
	menu.TrackPopupMenuEx(TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_RIGHTALIGN, rc.right, rc.bottom, gpFrame);
	ptb->SendMessage(TB_PRESSBUTTON, ID_CHARSET, MAKELONG(0, 0));
}
